package todlu.RssReader;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;

import org.xmlpull.v1.XmlPullParser;

import todlu.RssReader.RSS.RssFeed;
import todlu.RssReader.RSS.RssFeedList;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;
import android.util.Xml;
import android.webkit.WebSettings;



public class Settings {
	private final static String SETTING = "settings";
	private static Settings single = null;
	private File file = null;
	private static Context context = null;
	private static ShowImage showImg = ShowImage.ShowAllTheTime;
	private static WebSettings.TextSize webTextSize;
	private static SharedPreferences pref = null;
	private static boolean isPostCapture = true;
	private static boolean isPostLink = true;
	
	public enum ShowImage {ShowAllTheTime, ShowWifi, NoShow};
	
	private Settings(Context c)
	{
		this.context = c;
		//pref = context.getSharedPreferences(SETTING, Activity.MODE_WORLD_READABLE | Activity.MODE_WORLD_WRITEABLE);
		showImg = ShowImage.ShowAllTheTime;
		webTextSize = WebSettings.TextSize.NORMAL;
		
		loadSettings();
	}
	
	public static Settings getInstance(Context context)
	{
		if(single == null)
		{
			single = new Settings(context);
		}
		
		return single;
	}
	
	private void createDefaultSettings() {
		// TODO Auto-generated method stub
		showImg = ShowImage.ShowAllTheTime;
		webTextSize = WebSettings.TextSize.NORMAL;
		isPostCapture = true;
		isPostLink = true;
		
		saveSettings();
	}
	
	public static void saveSettings()
	{
		if(context == null)
		{
			return;
		}
		pref = context.getSharedPreferences(SETTING, context.MODE_WORLD_WRITEABLE);
		SharedPreferences.Editor editor = pref.edit();
		editor.putString("ShowImage", showImg.toString());
		editor.putString("WebTextSize", webTextSize.toString());
		editor.putBoolean("PostCapture", isPostCapture);
		editor.putBoolean("PostLink", isPostLink);
		editor.commit();
	}
	
	public static void loadSettings()
	{
		if(context == null)
		{
			return;
		}
		pref = context.getSharedPreferences(SETTING, context.MODE_WORLD_WRITEABLE);
		String tmp;
		
		tmp = pref.getString("ShowImage", "ShowAllTheTime");
		if(tmp.equalsIgnoreCase("ShowAllTheTime"))
		{
			showImg = ShowImage.ShowAllTheTime;
		}
		else if(tmp.equalsIgnoreCase("ShowWifi"))
		{
			showImg = ShowImage.ShowWifi;
		}
		else if(tmp.equalsIgnoreCase("NoShow"))
		{
			showImg = ShowImage.NoShow;
		}
		
		tmp = pref.getString("WebTextSize", "NORMAL");
		webTextSize = WebSettings.TextSize.valueOf(tmp);
		isPostCapture = pref.getBoolean("PostCapture", true);
		isPostLink = pref.getBoolean("PostLink", true);
	}
	
	public static void setShowImage(ShowImage show)
	{
		showImg = show;
	}
	
	public static ShowImage getShowImage()
	{
		return showImg;
	}
	
	public static void setWebTextSize(WebSettings.TextSize size)
	{
		webTextSize = size;
	}
	
	public static WebSettings.TextSize getWebTextSize()
	{
		return webTextSize;
	}
	
	public static boolean getIsPostCapture()
	{
		return isPostCapture;
	}
	
	public static void setIsPostCapture(boolean b)
	{
		isPostCapture = b;
	}
	
	public static void saveIsPostCapture()
	{
		if(context == null)
		{
			return;
		}
		pref = context.getSharedPreferences(SETTING, Context.MODE_WORLD_WRITEABLE);
		SharedPreferences.Editor editor = pref.edit();
		editor.putBoolean("PostCapture", isPostCapture);
		editor.commit();
	}
	
	public static void saveIsPostCapture(boolean b)
	{
		setIsPostCapture(b);
		saveIsPostCapture();
	}
	
	public static boolean getIsPostLink()
	{
		return isPostLink;
	}
	
	public static void setIsPostLink(boolean b)
	{
		isPostLink = b;
	}
	
	public static void saveIsPostLink()
	{
		if(context == null)
		{
			return;
		}
		pref = context.getSharedPreferences(SETTING, Context.MODE_WORLD_WRITEABLE);
		SharedPreferences.Editor editor = pref.edit();
		editor.putBoolean("PostLink", isPostLink);
		editor.commit();
	}
	
	public static void saveIsPostLink(boolean b)
	{
		setIsPostLink(b);
		saveIsPostLink();
	}

}